package com.example.administrator.yunniuusedcar.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.administrator.common.base.BaseActivity;
import com.example.administrator.yunniuusedcar.R;
import com.example.administrator.yunniuusedcar.fragment.ComprehensivePaiXuFragment;
import com.example.administrator.yunniuusedcar.fragment.PriceSortFragment;
import com.example.administrator.yunniuusedcar.fragment.SXScreenFragment;
import com.example.administrator.yunniuusedcar.fragment.SellFirstFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 *        平行进口2  （单选组） (radioGroup + Fragment )
 * 综合排序    销量   价格排序  筛选
  */

public class PingXingJinKou_CarActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_ZGHcar)
    TextView tvZGHcar;
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    @BindView(R.id.iv_shoopingCar)
    ImageView ivShoopingCar;
    @BindView(R.id.radiobutton_ComprehensiveSort)   // 综合排序
    RadioButton radiobuttonComprehensiveSort;
    @BindView(R.id.radiobuttonSellFirst)    // 销量
    RadioButton radiobuttonSellFirst;
    @BindView(R.id.radiobutton_PriceSort)   // 价格排序
    RadioButton radiobuttonPriceSort;
    @BindView(R.id.radiobutton_SXScreen)      //筛选
    RadioButton radiobuttonSXScreen;
    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;     /////////
    @BindView(R.id.fl_ZGhao_car)
    FrameLayout flZGhaoCar;
    private View mView;
    private Fragment[] frgs;

        private int currentIndex;

    //private HashMap    选车平台

    @Override
    public int getLayoutRes() {
        return R.layout.activity_details_car;
    }


    @Override
    public void initView() {
        ButterKnife.bind(this);

      // mView = View.inflate(this, R.layout.activity_details_car, null);

        initToolBar();
        initEvent();
    }

    //创建集合
    private void initEvent() {
        frgs = new Fragment[] {
          new ComprehensivePaiXuFragment() ,   // 综合排序
                new SellFirstFragment(),      // 销量
                new PriceSortFragment(),      // 价格排序
                new SXScreenFragment()        //筛选
        };
        //开启事务
        FragmentTransaction fts =  getSupportFragmentManager().beginTransaction();
        fts.add(R.id.fl_ZGhao_car, frgs[0]).commit();    // 设置为默认界面 //fl_ZGhao_car

        // 设置单选组 的监听事件 切换fragment
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                switch (checkedId){
                    case R.id.radiobutton_ComprehensiveSort:
                        setIndexSelecteds(0);
                        break;
                    case R.id.radiobuttonSellFirst :
                        setIndexSelecteds(1);
                        break;
                    case R.id.radiobutton_PriceSort :
                        setIndexSelecteds(2);
                        break;
                    case R.id.radiobutton_SXScreen :

                        setIndexSelecteds(3);
                        break;
                }
            }
        });
    }


    private void setIndexSelecteds(int index) {   // 设置fragment 界面
        if (currentIndex == index){
            return;
        }
        // 开启事务
        FragmentTransaction ftt = getSupportFragmentManager().beginTransaction();

        ftt.hide(frgs[currentIndex]);

        // 判断当前的fragment是否已添加
        if (!frgs[index].isAdded()){
            ftt.add(R.id.fl_ZGhao_car,frgs[index]).show(frgs[index]);  // 用事务区添加，省内存

        }else {
            ftt.show(frgs[index]);
        }
    }


    private void initToolBar() {

    }

    @OnClick({R.id.iv_back, R.id.iv_search, R.id.iv_shoopingCar, R.id.radiobutton_PriceSort, R.id.radiobutton_SXScreen})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:  // 返回上一级
                finish();
                break;
            case R.id.iv_search:  //搜索
                initSearch();
                break;
            case R.id.iv_shoopingCar:  // 购物车
                initGoShoopingCar();
                break;
            case R.id.radiobutton_PriceSort: //价格排序
                break;
            case R.id.radiobutton_SXScreen:  // 筛选
                break;
        }
    }

    private void initGoShoopingCar() {   // 购物车

    }

    private void initSearch() {     //搜索

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }
}
